/**
 * @param {number[][]} grid
 * @return {number}
 */

var maxValue = function (grid) {
    let row = grid.length
    let col = grid[0].length
    let dp = new Array(row)
    for (let i = 0; i < row; i++) {
        dp[i] = new Array(col)
    }
    for (let i = 0; i < row; i++) {
        for (let j = 0; j < col; j++) {
            // 左上角第一个元素
            if (i === 0 && j === 0) {
                dp[i][j] = grid[i][j]
            }
            // 第一行
            if (i === 0 && j > 0) {
                dp[i][j] = dp[i][j - 1] + grid[i][j]
            }
            // 第一列
            else if (i > 0 && j === 0) {
                dp[i][j] = dp[i - 1][j] + grid[i][j]
            }
            else if (i > 0 && j > 0) {
                dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]
            }
        }
    }
    return dp[row - 1][col - 1]
};
console.log(maxValue([
    [1, 3, 1],
    [1, 5, 1],
    [4, 2, 1]
]))